package thread;
// 自己来实现线程池


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPoolExecutor1{
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
    private List<Thread> list = new ArrayList<>();
    public MyThreadPoolExecutor1(int n){
        for(int i = 0; i < n; i++){
            Thread t = new Thread(() -> {
                while(true){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
            list.add(t);
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}


public class ThreadDemo29 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor1 executor1 = new MyThreadPoolExecutor1(5);
        for (int i = 0; i < 1000; i++) {
            int n = i;
            executor1.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("数字为:" + n + "线程名为：" + Thread.currentThread().getName());
                }
            });
        }
    }
}
